﻿import tkinter as tk
from tkinter import *
from tkinter import messagebox
import random
class GuessWater():
    def __init__(self,name):
        self.name = name
        self.NumberOfAttempts = 0
        self.NumberOfAttemptsBig = 0
        self.NumberOfAttemptsSmall = 0
        
    
    def MainWindowActivity(self,rnum):
        
        Label(self.name,text="  浇水量(L)：").place(x=210,y=10,width=60,height=30)
        
        WaterIn=StringVar()
        InputText = Entry(self.name,textvariable=WaterIn)
        InputText.place(x=280,y=10,width=50,height=30)
        
        Button(self.name,text='猜一猜',command=lambda:self.GuessLogic(InputText,rnum,textOutput)).place(x=210,y=50,width=120,height=30)
        textOutput = Text(self.name)
        textOutput.place(x=10,y=100,width=580,height=200)
        textOutput.insert('0.0', '小明要给树苗浇水，可是他不知道需要浇多少水，\n请你完成下面的猜数游戏，\n帮助小明找到浇水量吧！\n浇水量在1~100L之间哦！\n本程序以Apache 2.0开源协议开源在gitee\nhttps://gitee.com/cuteaplane/tree-water-guess')
        

    def GuessLogic(self, InputText, rnum,textOutput):
        self.i = InputText.get()
        global NumberOfAttempts, NumberOfAttemptsBig, NumberOfAttemptsSmall
        
        try:
            a = float(self.i)
            #print(a)
            #print(rnum) #DEBUG_CHEATING!
            if a > 100 or a < 1:
                messagebox.showerror('错误！', '浇水量在0--100之间哦！')
                WaterOutofRange = True
            else:
                WaterOutofRange = False
        except ValueError:
            messagebox.showerror('错误！', '请输入一个整数！')
            WaterOutofRange = True

        if WaterOutofRange:
            textOutput.delete('1.0', 'end')
            textOutput.insert('0.0', '浇水量在0--100之间哦，再试一次吧！')
        else:
            if a > rnum:
                textOutput.delete('1.0', 'end')
                textOutput.insert('0.0', '猜大了，再试一次吧！')
                self.NumberOfAttempts += 1
                self.NumberOfAttemptsBig += 1
            elif a < rnum:
                textOutput.delete('1.0', 'end')
                textOutput.insert('0.0', '猜小了，再试一次吧！')
                self.NumberOfAttempts += 1
                self.NumberOfAttemptsSmall += 1
            elif a == rnum:
                textOutput.delete('1.0', 'end')
                textOutput.insert('0.0', '猜对了，恭喜你！')
                self.NumberOfAttempts += 1
                messagebox.showinfo('恭喜你！', '恭喜你帮助了小明猜对了浇水量！\n小明谢谢你对环境事业做出的贡献！')

                # 统计信息的输出
                messagebox.showinfo('统计信息', f'共猜了：{self.NumberOfAttempts}次\n猜大了：{self.NumberOfAttemptsBig}次\n猜小了：{self.NumberOfAttemptsSmall}次')

            
        
def MainWindowStarter():
    rnum = random.randint(1,100)
    Window = tk.Tk()
    Window.title('猜猜小树需要浇多少水？')
    Window.geometry('600x300')
    info = GuessWater(Window)
    info.MainWindowActivity(rnum)
    Window.mainloop()
MainWindowStarter()


        
        
        
        
    

         
     
    


